$(document).ready(
		(function() {
			getNowUser();
			updateFollowersCount();
			updateConcersCount();
			var res = showAtRight(0, 1);

			$(document).on('click',".passage",
					function() {
						console.log($(this).find('a')[0]);
						var postId = $(this).find('a').attr('value');
						window.location.href = 'inner_post.html?postid='
								+ postId + '&pageNumber=1';
					});

			$("#settingList").mouseleave(function() {
				$("#settingList").hide();
			});

			$(document).on('click', ".zan", function() {
				postid = $(this).parent().attr('value')
				if ($(this).text() == '已赞') {
					cancelAgree($(this).parent(), postid);
				} else {

					agree($(this).parent(), postid);
				}
			});

			$(document).on('click', ".collect", function() {
				postid = $(this).parent().attr('value')
				if ($(this).text() == '已收藏') {
					cancelCollect($(this).parent(), postid);

				} else {
					collect($(this).parent(), postid);

				}
			});
		}));

function showList() {
	$("#settingList").show();
}

function getPostCount(topicId) {
	var res = 0
	$.ajax({
		url : 'post/getTopicPostsTotalNumber',
		type : "GET",
		async : false,
		data : {
			topicid : topicId
		},
		success : function(data) {
			console.log(data['status']);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				res = data['result']['totalNum'];
				break;
			default:
				alert(data['msg']);
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;
}

function createPage(count, start, topicId) {

	if (count < 5) {
		display = count;
	} else {
		display = 5;
	}
	$("#page").paginate({
		count : count,
		start : start,
		display : display,
		border : true,
		border_color : '#BEF8B8',
		text_color : '#68BA64',
		background_color : '#E3F2E1',
		border_hover_color : '#68BA64',
		text_hover_color : 'black',
		background_hover_color : '#CAE6C6',
		rotate : false,
		images : false,
		mouse : 'press',
		onChange : function(page_index) {
			showAtRight(topicId, page_index);
		}
	});
}

function showAtRight(topicId, pageNumber) {

	$("#posts").empty();
	$("#page").empty();

	var count = getPostCount(topicId);
	totalPage = Math.ceil(count / 3);
	if (totalPage == 0) {
		$("#posts").append(
				'<div><h4 style="text-align:center">该版块暂无帖子</h4></div>');
		return;
	}
	posts = getPosts(topicId, pageNumber);
	console.log(posts);
	for (i = 0; i < posts.length; i++) {
		post = posts[i];
		if (post['isAgreed'] == true) {
			var isAgree = '已赞'
		} else {
			var isAgree = '赞'
		}
		if (post['isCollected'] == true) {
			var isCollected = '已收藏'
		} else {
			var isCollected = '收藏'
		}

		container = '<div class="container"> '
				+ ' <div class="user-image"> '
				+ ' <a href="#"><img src= ' + post['publishUser']['headImg'] + ' id="userImage" style="width:30px,height:20px;" '
				+ ' width="30" height="30"></a> </div> '
				+ ' <div class="user-name"> '
				+ ' <a id="userName" href="other_homepage.html?userid='
				+ post['publishUser']['userid']
				+ '">'
				+ post['publishUser']['userName']
				+ '</a> </div> '
				+ ' <div class="passage"> '
				+ ' <a href="#" value='
				+ post['post']['postid']
				+ ' id="passageName">'
				+ post['post']['title']
				+ '</a> '
				+ ' <p id="date">'
				+ post['post']['time']
				+ '</p> '
				+ '  <p id="passageMain"> '
				+ post['post']['txtContent']
				+

				' </div> '
				+ ' <br> '
				+ ' <div class="btnCol" value='
				+ post['post']['postid']
				+ '> <button  class="zan">'
				+ isAgree
				+ '</button> '
				+ '<button class="zanNum">'
				+ post['agreeNum']
				+ '人已赞</button>'
				+ '<button  class="collect">'
				+ isCollected
				+ '</button>' + '</div> '
		' </div> ';

		$("#posts").append(container);

	}
	console.log(posts);

	if (totalPage > 1) {
		createPage(totalPage, pageNumber, topicId);
	}

}

function getNowUser() {
	$.ajax({
		url : 'user/getNowUser',
		type : "GET",
		data : {},
		success : function(data) {
			console.log(data);
			if (data['status'] == -250) {
				window.location.href = "login.html";
			} else if (data['status'] == 0) {
				$("#myUserName").text(data['result']['nowUser']['userName']);
				$("#avatar").attr("src", data['result']['nowUser']['headImg'])
			} else {
				alert(data['msg']);
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
}

function updateFollowersCount() {
	followerCount = getFollowCounts(0);
	$("#fansNum").text(followerCount);
}

function updateConcersCount() {
	concersCount = getConcernCounts(0);
	$("#followNum").text(concersCount);
}

// 点赞帖子
function agree(element, postid) {
	$.ajax({
		url : 'post/agreePost',
		type : "POST",
		data : {
			postid : postid
		},
		success : function(data) {
			console.log(data);
			if (data['status'] == -250) {
				window.location.href = "login.html";
			} else if (data['status'] == 0) {
				element.children('.zanNum').text(
						(Number(element.children('.zanNum').text()[0]) + 1)
								+ '人已赞');
				element.children('.zan').text('已赞');
			} else {
				alert(data['msg']);
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
}

// 取消点赞
function cancelAgree(element, postid) {
	$.ajax({
		url : 'post/cancelAgreePost',
		type : "POST",
		data : {
			postid : postid
		},
		success : function(data) {
			console.log(data);
			if (data['status'] == -250) {
				window.location.href = "login.html";
			} else if (data['status'] == 0) {
				element.children('.zanNum').text(
						(Number(element.children('.zanNum').text()[0]) - 1)
								+ '人已赞');
				element.children('.zan').text('赞');
			} else {
				alert(data['msg']);
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
}

// 收藏帖子
function collect(element, postid) {
	$.ajax({
		url : 'post/collectPost',
		type : "POST",
		data : {
			postid : postid
		},
		success : function(data) {
			console.log(data);
			if (data['status'] == -250) {
				window.location.href = "login.html";
			} else if (data['status'] == 0) {
				element.children('.collect').text('已收藏');
			} else {
				alert(data['msg']);
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
}

// 取消收藏帖子
function cancelCollect(element, postid) {
	$.ajax({
		url : 'post/cancelCollectPost',
		type : "POST",
		data : {
			postid : postid
		},
		success : function(data) {
			console.log(data);
			if (data['status'] == -250) {
				window.location.href = "login.html";
			} else if (data['status'] == 0) {
				element.children('.collect').text('收藏');
			} else {
				alert(data['msg']);
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
}

//搜索
function searchByKeyword() {
	var keyWord = $("#search").val();
	window.location.href = 'search_result.html?keyWord='
		+ keyWord;
}